//
//  HeaderView.swift
//  知乎日报Demo
//
//  Created by Ann on 2017/10/17.
//  Copyright © 2017年 Ann. All rights reserved.
//

import UIKit
import Alamofire
import Kingfisher
protocol HeaderViewDelegate:NSObjectProtocol {
    func showDetialView(indexOf:Int)
}

 //Step.3 HeaderView 完善
class HeaderView: UIView,UIScrollViewDelegate{
    
    var topNewsArray:[Status]?
    var headerViewDelegate:HeaderViewDelegate?
    var imageViewArrayOverAll = [UIImageView]()
    
    lazy var pageView:UIPageControl = {
        let pv = UIPageControl()
        return pv
    }()
   
    lazy var topScrollView:UIScrollView = {
    let sc = UIScrollView()
        sc.backgroundColor = UIColor.green
    return sc
    }()
 
    init(topNewsArray:[Status]?) {
 
        super.init(frame: CGRect.zero)
      
        guard topNewsArray != nil else {
            return
        }
    
        self.topNewsArray = topNewsArray
         
        //3.1 ScrollView初始化
        setScrollInView(topNewsArray: topNewsArray)
        //3.2 添加PageView
        setupPageView(count: topNewsArray!.count)
        
        pageChange()
        
    }
    
    
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
      let num =  floor(scrollView.contentOffset.x / scrollView.frame.size.width)
    
        let page = Int(floor((scrollView.contentOffset.x - scrollView.frame.size.width / 2 ) / scrollView.frame.size.width)) + 1
       
        self.pageView.currentPage = page
        
    }
    func pageChange(){
        
     topScrollView.delegate = self as? UIScrollViewDelegate
    }
    
    func setupPageView(count:Int){
        pageView.numberOfPages = count
        
        pageView.currentPage = 0
        self.addSubview(pageView)
        
        pageView.sizeToFit()
        pageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.snp.centerX)
            make.bottom.equalTo(self.snp.bottom)
        }
        
    }
    
    func setScrollInView(topNewsArray:[Status]?){
        
        self.addSubview(topScrollView)
        topScrollView.isUserInteractionEnabled = true
        topScrollView.isScrollEnabled = true
        topScrollView.isPagingEnabled = true
        topScrollView.showsHorizontalScrollIndicator = false
        topScrollView.showsVerticalScrollIndicator = false
        
        topScrollView.isScrollEnabled = true
        topScrollView.scrollsToTop = false
        topScrollView.backgroundColor = UIColor.darkGray
        
        guard let topNews = topNewsArray else {
            
            return
            
        }
        
        self.topNewsArray = topNews
        
        
        let imageViewCount = CGFloat(topNews.count)
        
        topScrollView.contentSize = CGSize(width: UIScreen.main.bounds.width * imageViewCount, height: 300)
        
        let windowWidth = UIScreen.main.bounds.width
        
        topScrollView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 300)
        
        var imageViewArray = [UIImageView]()
        
        for i in 0 ..< Int(imageViewCount) {
            
            let imgV = UIImageView(frame:CGRect(x: windowWidth * CGFloat(i), y: 0, width: windowWidth, height: 300))
            
            imgV.backgroundColor = UIColor.ann_randomColor()
            
            let imgTemp =  topNews[i].image
            
            guard let imgURL = imgTemp else {
                return
            }
            
            imgV.kf.setImage(with: URL(string: imgURL))
            imgV.isUserInteractionEnabled = true
            imgV.contentMode = UIViewContentMode.scaleToFill
            imgV.tag = i
       let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.imagePressed ))
            
            tap.numberOfTapsRequired = 1
            tap.numberOfTouchesRequired = 1
            imgV.addGestureRecognizer(tap)
       
            topScrollView.addSubview(imgV)
            imageViewArray.append(imgV)
          
        }
        self.imageViewArrayOverAll = imageViewArray
        setupTitleLabel()
        
     
        setMaskView()

        
        
        
    }
    func imagePressed ( tap:UITapGestureRecognizer){
 
        headerViewDelegate?.showDetialView(indexOf: tap.view!.tag)
     
    }
        required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        }
        func setupTitleLabel(){

        var i = 0
        for view in imageViewArrayOverAll {

        let titleLabel = UILabel()
        titleLabel.font = UIFont.boldSystemFont(ofSize: 22)
        titleLabel.textColor = UIColor.white
        titleLabel.text = topNewsArray?[i].title
        titleLabel.numberOfLines = 0
        view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints({ (make) in
        make.bottom.equalTo(view.snp.bottom)
        make.left.equalTo(view.snp.left)
        make.right.equalTo(view.snp.right)
        make.height.equalTo(view.bounds.height / CGFloat(3))
        })
        i += 1

        }

        }
        func setMaskView(){
        var i = 0
        for view in imageViewArrayOverAll {

        let imgv = UIImageView()

        imgv.image = UIImage(named: "Home_Image_Mask")
        view.insertSubview(imgv, at: 0)
        imgv.snp.makeConstraints({ (make) in
        make.bottom.equalTo(view.snp.bottom)
        make.left.equalTo(view.snp.left)
        make.right.equalTo(view.snp.right)
        make.height.equalTo(view.bounds.height / CGFloat(2))
        })
        i += 1

        }
    }

}
 
